package com.example.ass7.animation;

import java.awt.Color;

import biuoop.DrawSurface;
import biuoop.Sleeper;

import com.example.ass7.geometryprimitives.Counter;
import com.example.ass7.sprite.Sprite;
import com.example.ass7.sprite.SpriteCollection;

//The CountdownAnimation will display the given gameScreen,
//for numOfSeconds seconds, and on top of them it will show
//a countdown from countFrom back to 1, where each number will
//appear on the screen for (numOfSeconds / countFrom) secods, before
//it is replaced with the next one.
/**
 * @author Meirav Ben Izhak
 */
public class CountdownAnimation implements Animation {

    private final Counter countDown;
    private double numOfSeconds;
    private final SpriteCollection gameScreen;
    private String text;
    private final Sleeper sleeper;

    /**
     * @param numOfSeconds
     *            numOfSeconds
     * @param countFrom
     *            countFrom
     * @param gameScreen
     *            gameScreen
     */
    public CountdownAnimation(
        double numOfSeconds, int countFrom, SpriteCollection gameScreen) {
        this.numOfSeconds = numOfSeconds;
        this.countDown = new Counter();
        this.countDown.increase(countFrom);
        this.gameScreen = gameScreen;
        this.text = "";
        this.sleeper = new Sleeper();
    }

    @Override
    public void doOneFrame(DrawSurface d,
            double dt) {

        for (Sprite sprite : this.gameScreen.getSpritesList()) {
            sprite.drawOn(d);
        }

        d.setColor(Color.WHITE);
        if (this.countDown.getValue() > 0) {
            this.text = String.valueOf(this.countDown.getValue());
        }
        d.drawText(400, 300, this.text, 30);

        if (this.countDown.getValue() == 0) {
            this.text = "GO";
            d.drawText(400, 300, this.text, 30);
            this.sleeper.sleepFor(100);
        }
        this.sleeper.sleepFor(700);
    }

    @Override
    public boolean shouldStop() {
        if (this.numOfSeconds > 0) {
            this.countDown.decrease(1);
            this.numOfSeconds--;
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

}